home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / date.arc / SETTIME.PAS < prev   
Pascal/Delphi Source File  |  1980-01-01  |  6KB  |  224 lines

  1. program Set_Time(input,output);
  2. {
  3.    Version 1.03b for Turbo Pascal version 2.00b PC-DOS
  4.  
  5.    Program to set the system clock.
  6.    Written and Copyright 1985 by Tim MacNary
  7.    Permission to use is granted provided this notice remains.
  8.  
  9.    Included are system routines to set and get the system clock
  10.    and to get single, non-echoed characters from the keyboard.
  11.  
  12.    A description of the operation of this program is part of
  13.    procedure help.
  14.  
  15. }
  16. const
  17.    abort = 27;      { the abort hotkey }
  18.    toggle = 9;      { toggle key for AM/PM }
  19.    helpkey= 63;     { key to display helpMsg }
  20.    helpMsg = 'Esc to abort, Tab to toggle AM/PM, ? for help';
  21.  
  22. type regtype = record
  23.         ax,bx,cx,dx,bp,si,di,ds,es,flags:integer;
  24.      end;
  25.    DayType = (AM,PM);
  26.    RangeType = set of 27..128;       { the possible set of input values }
  27. var
  28.    Co:integer;
  29.    hour,min,sec,hun:byte;
  30.    day:dayType;               { AM/PM indicator }
  31.    InputRange:RangeType;      { set of acceptable keystrokes for input }
  32.  
  33. procedure ReadAllChars(var InputNum:integer;var ExtendedAscii:boolean);
  34.  
  35. { Get an unbuffered, single character from the keyboard using BIOS call 16H }
  36.  
  37. var
  38.    Reg:RegType;
  39. begin
  40.    Reg.AX:=$0000;
  41.    intr($16,Reg);
  42.    if LO(Reg.AX)=00 then
  43.    begin
  44.       InputNum:=HI(Reg.AX);
  45.       ExtendedAscii:=true;
  46.    end
  47.    else begin
  48.       InputNum:=LO(Reg.AX);
  49.       ExtendedAscii:=false;
  50.    end;
  51. end;
  52.  
  53. procedure print_Time(day:dayType;hour,min,sec,hun:byte;px,py:integer);
  54.  
  55. { Print out the time, centered on line 3, then return the cursor to its
  56.   last position. }
  57.  
  58. var x,y:integer;
  59. begin
  60.    x:=whereX;
  61.    y:=whereY;
  62.    GotoXY(px,py);
  63.    write(hour:2,':');
  64.    if min < 10 then write('0');
  65.    write(min:1);
  66.    {
  67.    write(':');
  68.    if sec < 10 then write('0');
  69.    write(sec:1,'.');
  70.    if hun < 10 then write('0');
  71.    write(hun:1,'       ');
  72.    }
  73.    if day = AM then write(' AM') else write(' PM');
  74.    GotoXY(x,y);
  75. end;
  76.  
  77. procedure Set_Time(day:dayType;hour,min,sec,hun:byte);
  78.  
  79. { System routine to set the system clock. }
  80.  
  81. var regs:regtype;
  82. begin
  83.    regs.ax:=$2D00;
  84.    if (day=PM) and (hour <> 12) then hour:=hour + 12;
  85.    if (day=AM) and (hour = 12) then hour:=0;
  86.    regs.cx:=hour * $100 + min;
  87.    regs.dx:=(sec * $100) + hun;
  88.    intr($21,regs);
  89.    if lo(regs.ax) <> 0  then write('Error during set time');
  90. end;
  91.  
  92. procedure Get_Time(var day:dayType;var hour,min,sec,hun:byte);
  93.  
  94. { System routine to get the system time. }
  95.  
  96. var regs:regtype;
  97. begin
  98.    regs.ax:=$2C00;
  99.    intr($21,regs);
  100.    hour:=hi(regs.cx);
  101.    min:=lo(regs.cx);
  102.    sec:=hi(regs.dx);
  103.    hun:=lo(regs.dx);
  104.    if hour in [12..23] then day:=PM else day:=AM;
  105.    if hour > 12 then hour:=hour - 12;
  106.    if hour = 0 then hour := 12;
  107. end;
  108.  
  109. procedure set_up;
  110. var Day:daytype;
  111.     hour,min,sec,hun:byte;
  112.     co:integer;
  113. begin
  114.    ClrScr;
  115.    LowVideo;
  116.    co:=length(helpmsg);
  117.    co:=40 - (co div 2);
  118.    GotoXY(co,10);
  119.    write(helpMsg);
  120.  
  121.    Get_Time(Day,hour,min,sec,hun);
  122.    gotoXY(20,1);   write('Current Time:');
  123.    print_Time(Day,hour,min,sec,hun,35,1);
  124.    NormVideo
  125. end;
  126.  
  127. procedure help;
  128. var x,y:integer;
  129. begin
  130.    x:=whereX;   y:=whereY;
  131.    GotoXY(1,11);
  132.    LowVideo;
  133.    writeln('         To set the time, three keys must be hit:one for the hour, one');
  134.    writeln('      for the ten''s place of the minute, and one for the one''s place.');
  135.    writeln('      Only one key is pressed for the hour; the top row of the keyboard');
  136.    writeln('      is used as follows');
  137.    writeln;
  138.    NormVideo;
  139.    writeln('              ┌───╥───╥───╥───╥───╥───╥───╥───╥───╥───╥───╥───┐');
  140.    writeln('              │ 1 ║ 2 ║ 3 ║ 4 ║ 5 ║ 6 ║ 7 ║ 8 ║ 9 ║ 0 ║ - ║ = │');
  141.    writeln('              └───╨───╨───╨───╨───╨───╨───╨───╨───╨───╨───╨───┘');
  142.    writeln('          hours:1   2   3   4   5   6   7   8   9   10  11  12');
  143.    writeln;
  144.    LowVideo;
  145.    writeln('      Example: The time desired is 12:03 AM. These keys must be pressed:');
  146.    writeln('      =,0,3.  If PM is needed, press the tab key.');
  147.    writeln;
  148.    writeln('      The Tab key toggles between AM and PM at any time; Esc aborts.');
  149.    NormVideo;
  150.    gotoXY(x,y);
  151. end;
  152.  
  153. procedure Get_Input(var day:daytype;hour,min,sec,hun:integer;
  154.                     var co:integer;InputRange:RangeType);
  155.  
  156. { Routine to get a single character, looping until the character is in
  157.   the set InputRange. Additionally, toggles day between AM/PM, updating
  158.   the time display when changed. }
  159.  
  160. var  extAscii:boolean;
  161. begin
  162.    repeat
  163.       ReadAllChars(co,extascii);
  164.       if co=toggle then
  165.       begin
  166.          if Day = AM then Day := PM
  167.          else Day := AM;
  168.          print_Time(Day,hour,min,sec,hun,35,3);
  169.       end
  170.       else if co = helpkey then
  171.       begin
  172.          help;
  173.          print_Time(Day,hour,min,sec,hun,35,3)
  174.       end
  175.    until (co in InputRange) and not extAscii;
  176. end;
  177.  
  178. begin { Main Program }
  179.    Set_Up;
  180.    Day:=AM;   hour:=0;   min:=0;   sec:=0;   hun:=0;
  181.    print_Time(Day,hour,min,sec,hun,35,3);
  182.  
  183.    InputRange:=[48..57,61,45,abort];
  184.    gotoXY(35,4);   write(' - hours     ');
  185.    Get_Input(Day,hour,min,sec,hun,co,InputRange);
  186.    if co <> abort then
  187.    begin
  188.       if co in [49..57] then hour:=co - 48
  189.       else case co of
  190.          48:hour:=10;
  191.          45:hour:=11;
  192.          61:hour:=12
  193.       end;
  194.       print_Time(Day,hour,min,sec,hun,35,3);
  195.       InputRange:=[48..53,abort];
  196.       gotoXY(35,4);   write('   - minutes ');
  197.       Get_Input(Day,hour,min,sec,hun,co,InputRange);
  198.       if co <> abort then
  199.       begin
  200.          min:=(co - 48 ) * 10;
  201.  
  202.          print_Time(Day,hour,min,sec,hun,35,3);
  203.          InputRange:=[48..57,abort];
  204.          gotoXY(35,4);   write('    - minutes ');
  205.          Get_Input(Day,hour,min,sec,hun,co,InputRange);
  206.          if co <> abort then
  207.          begin
  208.             min:=min + (co - 48);
  209.  
  210.             print_Time(Day,hour,min,sec,hun,35,3);
  211.             gotoXY(34,3);   write('<');gotoXY(43,3);write('>');
  212.             gotoXY(35,4);   write('Time Set           ');
  213.             gotoXY(1,9);   ClrEOL;
  214.             gotoXY(1,10);  ClrEOL;
  215.             gotoXY(1,1);   ClrEOL;
  216.             Set_Time(Day,hour,min,sec,hun);
  217.          end
  218.          else write('Aborted')
  219.       end
  220.       else write('Aborted')
  221.    end
  222.    else write('Aborted')
  223. end.
  224.